package com.core.family.biz.yizhao.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.core.common.gen.BaseQuery;
import com.core.common.utils.ValidationUtil;
import com.core.family.biz.yizhao.domain.po.YzOrderPrice;
import com.core.family.biz.yizhao.domain.po.YzOrderTake;
import com.core.family.biz.yizhao.domain.po.YzPriceItem;
import com.core.family.biz.yizhao.mapper.YzOrderPriceMapper;
import com.core.family.biz.yizhao.service.YzOrderPriceService;
import com.core.family.biz.yizhao.service.YzOrderTakeService;
import com.core.family.biz.yizhao.service.YzPriceItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.CollectionUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;

import com.core.common.gen.BaseServiceImpl;
import org.springframework.validation.ValidationUtils;


/**
 * @author honglei
 * @version 1.0
 * @time: 2023-7-7 23:20:46
 * @Description 服务实现层 一招报价表
 */
@Slf4j
@Service
public class YzOrderPriceServiceImpl extends BaseServiceImpl<YzOrderPriceMapper, YzOrderPrice> implements YzOrderPriceService {
    @Autowired
    YzOrderTakeService takeService;
    @Autowired
    YzPriceItemService itemService;

    @Override
    public List<YzOrderPrice> selectList(YzOrderPrice yzOrderPrice, BaseQuery queryBean, boolean copyBean) {
        List<YzOrderPrice> yzOrderPrices = super.selectList(yzOrderPrice, queryBean, copyBean);
        for (YzOrderPrice orderPrice : yzOrderPrices) {
            orderPrice.setItemList(itemService.getByPriceId(orderPrice.getId()));
        }
        return yzOrderPrices;

    }


    @Override
    public boolean saveOrUpdate(YzOrderPrice entity) {
        ValidationUtil.validateObject(entity);
        itemService.removeByPriceId(entity.getId());
        boolean isNew=false;
        if (Objects.nonNull(entity.getId())){
            isNew=true;
        }
         super.saveOrUpdate(entity);
         if (Objects.nonNull(entity.getItemList())){
             for (YzPriceItem o : entity.getItemList()) {
                 o.setPriceId(entity.getId());
             }
             itemService.saveBatch(entity.getItemList());
         }

         if (isNew){
             YzOrderTake take=new YzOrderTake()
                     .setOrderId(entity.getOrderId())
                     .setUserId(entity.getUserId())
                     .setStatus("报价中")
                     .setOrderTakePhone(entity.getMasterHone())
                     .setOrderTakeName(entity.getMasterName())
                     .setOrderTakeRolename(entity.getMasterRolename())
                     .setOrderPriceId(entity.getId());
             takeService.save(take);
         }

         return true;
    }

    @Override
    public YzOrderPrice getWithDetail(Integer id) {
        YzOrderPrice orderPrice=getById(id);
        if (Objects.nonNull(orderPrice)){
            orderPrice.setItemList(itemService.getByPriceId(orderPrice.getId()));
        }
        return orderPrice;
    }
}

